package cn.itliuliu.org.service.impl;


import cn.itliuliu.basic.constants.Constant;
import cn.itliuliu.basic.service.impl.BaseServiceImpl;
import cn.itliuliu.org.domain.Sdydetail;
import cn.itliuliu.org.mapper.SdydetailMapper;
import cn.itliuliu.org.service.ISdydetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SdydetailServiceImpl extends BaseServiceImpl<Sdydetail> implements ISdydetailService {

    @Autowired
    private RedisTemplate<Object, Object> redisTemplate;

    @Autowired
    private SdydetailMapper sdydetailMapper;


    @Override
    @Transactional
    public void add(Sdydetail sdydetail) {
        sdydetailMapper.insert(sdydetail);
    }

    @Override
    @Transactional
    @CacheEvict(cacheNames = Constant.DEPT_TREE_KEY, key = "'tree'")
    public void update(Sdydetail sdydetail) {
        sdydetailMapper.update(sdydetail);
    }

    @Override
    @Transactional
    @CacheEvict(cacheNames = Constant.DEPT_TREE_KEY, key = "'tree'")
    public void delete(Long id) {
        sdydetailMapper.delete(id);
    }


    @Override
    @Transactional
    @CacheEvict(cacheNames = Constant.DEPT_TREE_KEY, key = "'tree'")
    public void patchDelete(List<Long> ids) {
        sdydetailMapper.patchDelete(ids);
    }

    @Override
    public List<Sdydetail> queryTree() {
        return sdydetailMapper.loadAll();
    }


//    /**
//     * 无限级部门树如何查询
//     * 全部查出来重新组装数据【掌握】*
//     *
//     * @return
//     */
//    @Override
//    @Cacheable(cacheNames = Constant.DEPT_TREE_KEY, key = "'tree'")
//    public List<Department> queryTree() {
//        return getDeptTreeFromDb();
//    }
//
//
//    private List<Department> getDeptTreeFromDb() {
//        //查询出所有的dept
//        List<Department> allDepts = departmentMapper.loadAll();
//
//        //结果集
//        List<Department> resultDept = new ArrayList<>();
//
//        for (Department dept : allDepts) {
//            //找出所有的爹
//            if (dept.getParent_id() == null) {
//                resultDept.add(dept);
//            } else {
//                //找出所有的儿子塞到爹的children里 dept在这里就是子部门了
//                for (Department parent : allDepts) {
//                    if (parent.getId().equals(dept.getParent_id())) {
//                        //把儿子塞到爹的children里去
//                        parent.getChildren().add(dept);
//                    }
//                }
//            }
//        }
//        return resultDept;
//    }
}
